package com.youpai.platform.chat.service.util;

import com.youpai.platform.common.constant.NumberConstant;
import com.youpai.platform.common.enums.ProcessCodeEnum;
import com.youpai.platform.common.exceptions.BaseParamException;
import com.youpai.platform.vulture.common.enums.AppIdEnum;
import org.apache.commons.lang3.StringUtils;

/**
 * @date: 2022/7/16 17:38
 * @author: Anna
 * @version: 1.0
 * @description: util
 */
public class ChatUtils {

    /**
     * 构建accId
     *
     * @param appId appId
     * @param uid   uid
     * @return accId
     */
    public static String buildAccId(Integer appId, String uid) {
        if (null == appId || StringUtils.isBlank(uid)) {
            throw new BaseParamException(ProcessCodeEnum.PARAM_ERROR);
        }
        return appId + "_" + uid;
    }

    /**
     * 构建sessionId
     *
     * @param fromAccId 发送方accId
     * @param toAccId   接收方accId
     * @return sessionId
     */
    public static String buildSessionId(String fromAccId, String toAccId) {
        fromAccId = fromAccId.trim();
        toAccId = toAccId.trim();
        if (StringUtils.isBlank(fromAccId) || StringUtils.isBlank(toAccId)) {
            throw new BaseParamException(ProcessCodeEnum.PARAM_ERROR);
        }
        String sessionId;
        if (fromAccId.compareTo(toAccId) >= 0) {
            sessionId = fromAccId + "|" + toAccId;
        } else {
            sessionId = toAccId + "|" + fromAccId;
        }
        return sessionId;
    }


    /**
     * 获取SessionId中的另一个accId
     *
     * @param sessionId 会话id
     * @param accId     其中一个已知的accId
     * @return 另一个accId
     */
    public static String getAnotherAccId(String sessionId, String accId) {
        if (StringUtils.isBlank(sessionId) || StringUtils.isBlank(accId)) {
            throw new BaseParamException(ProcessCodeEnum.PARAM_ERROR);
        }
        String[] result = sessionId.split("\\|");
        if (NumberConstant.TWO_POSITIVE != result.length) {
            throw new BaseParamException(ProcessCodeEnum.PARAM_ERROR);
        }
        for (String str : result) {
            if (!accId.equals(str)) {
                return str;
            }
        }
        // 如果走到这里说明sessionId是由两个自己的id组成的
        return result[0];
    }

    /**
     * 通过sessionId获取accId数组
     *
     * @param sessionId 会话id
     * @return accId
     */
    public static String[] getAccIdsBySessionId(String sessionId) {
        if (StringUtils.isBlank(sessionId)) {
            throw new BaseParamException(ProcessCodeEnum.PARAM_ERROR);
        }
        String[] result = sessionId.split("\\|");
        if (NumberConstant.TWO_POSITIVE != result.length) {
            throw new BaseParamException(ProcessCodeEnum.PARAM_ERROR);
        }
        return result;
    }

    /**
     * 通过accId查询uid
     *
     * @return uid
     */
    public static String getUidByAccId(String accId) {
        if (StringUtils.isBlank(accId)) {
            return "";
        }
        final String[] strings = accId.split("_");
        if (strings.length != 2) {
            return "";
        }
        return strings[1];
    }

    /**
     * 通过uid和appId获取accId
     *
     * @param uid   用户uid
     * @param appId appId
     * @return accId
     */
    public static String getAccIdByUidAndAppId(String uid, Integer appId) {
        return appId + "_" + uid;
    }
}